package com.fnd.med.zsms.dao.impl;

import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Restrictions;

import com.fnd.med.utils.BaseDao;
import com.fnd.med.zsms.bean.App;
import com.fnd.med.zsms.bean.AppVersion;
import com.fnd.med.zsms.dao.AppVersionDao;

@Named("appVersionDao")
public class AppVersionDaoImpl extends BaseDao implements AppVersionDao {
	@Override
	public AppVersion findAppVersion(String appName) {
		
		Criteria criteria = getSession().createCriteria(App.class).add(Restrictions.eq("appName", appName));
		App app = (App) criteria.uniqueResult();
		String sql = "select * from ( select * from appversion where appSeq in "
				+ "(select seq from app where seq =:appSeq)) a where a.appVersion=(select "
				+ "max(b.appVersion)from(select * from appversion where appSeq in "
				+ "(select seq from app where seq =:appSeq)) b)";
		SQLQuery sqlQuery = getSession().createSQLQuery(sql);
		sqlQuery.setInteger("appSeq", app.getSeq());
		sqlQuery.addEntity(AppVersion.class);
		return (AppVersion) sqlQuery.uniqueResult();
	}

	@Override
	public void Save(AppVersion appVersion) {
		getSession().save(appVersion);
		
	}
	
	

}
